﻿<%@ Control Language="C#" AutoEventWireup="true" CodeFile="SmackTalk.ascx.cs" Inherits="SmackTalk" %>

<script type="text/javascript">
    $(function () {
        // Declare a proxy to reference the hub. 
        var chat = $.connection.chatHub;
        var userId = $(<%= userID.ClientID %>).val(),
        weekID = $(<%= weekID.ClientID %>).val();

        var scrollToBottomOfChat = function () {
            var objDiv = document.getElementById('<%= lstMessage.ClientID %>');
            objDiv.scrollTop = objDiv.scrollHeight;
        };

        scrollToBottomOfChat();
        // Create a function that the hub can call to broadcast messages.
        chat.client.broadcastMessage = function (name, date, message, week) {
            if (week == weekID) {
                // Add the message to the page. 
                $(<%= lstMessage.ClientID %>).append('<div><span class="user">' + name + '</span><span class="messagedate">(' + date + ')</span><span class="message">:   ' + message + '</span></div>');
                scrollToBottomOfChat();
            }
        };

        chat.client.broadcastComputerMessage = function (message, week, style) {
            if (week == weekID) {
                // Add the message to the page. 
                $(<%= lstMessage.ClientID %>).append('<div><span class="message ' + style + '">:   ' + message + '</span></div>');
                scrollToBottomOfChat();
            }
        };

        var sendMessage = function () {
            var text = $('#txtbox').val();
            // Call the Send method on the hub. 
            if (text && userId) {
                chat.server.sendMessage(userId, weekID, text);
                // Clear text box and reset focus for next comment. 
                $('#txtbox').val('').focus();
            }
        };

        $.connection.hub.start().done(function () {
            $(<%= sendmessage.ClientID %>).click(sendMessage);
        });


        $('#txtbox').keyup(function (e) {
            if (e.keyCode == 13 && userId) {
                sendMessage();
                return false;
            }
        });

        $(window).keydown(function (event) {
            if (event.keyCode == 13) {
                event.preventDefault();
                return false;
            }
        });
    });
    </script>
<asp:HiddenField runat="server" ID="weekID" />
<asp:HiddenField runat="server" ID="userID" />

<div id="smacktalk">
<p class="header">Smack Talk</p>

<div id="lstMessage" runat="server" class="smacktalkMessages">

</div>
<input placeholder="Enter Message" style="width:600px" id="txtbox"/> <input runat="server" type="button" id="sendmessage" value="Send" />
</div>